package main

import (
    "flag"
    "log"
    "time"

    "gitee.com/vim_z/downloader/internal"
)

var (
    url      = flag.String("url", "https://studygolang.com/dl/golang/go1.21.4.linux-amd64.tar.gz", "download url")
    workers  = flag.Int("work", 5, "works number")
    filename = flag.String("file", "go1.21.4.linux-amd64.tar.gz", "downloaded filename")
)

func main() {
    flag.Parse()
    start := time.Now()
    probe := internal.NewProbe(*workers, *url)
    size := probe.GetFileSize()
    parts := make(chan internal.Part, *workers)
    downloader := internal.NewDownloader(parts, size, *workers)
    for i := 0; i < *workers; i++ {
        go downloader.Download(i, *url)
    }
    if err := downloader.Merge(*filename); err != nil {
        log.Fatalln(err)
    }
    log.Printf("cost time %f second", time.Now().Sub(start).Seconds())
}
